package com.ui.zhong.webview

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.fragment.app.Fragment
import com.ui.zhong.Constant

class WebViewFragment : Fragment() {
    private lateinit var mUrl: String
    private var mNeedRefresh = true

    companion object {
        fun newInstance(url: String?, needRefresh: Boolean): WebViewFragment {
            val fragment = WebViewFragment()
            val bundle = Bundle()
            bundle.putString(Constant.WEB_VIEW_FRAGMENT_URL, url)
            bundle.putBoolean(Constant.WEB_VIEW_FRAGMENT_NEED_REFRESH, needRefresh)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val bundle = arguments
        if (bundle != null) {
            mUrl = bundle.getString(Constant.WEB_VIEW_FRAGMENT_URL) ?: "url is null"
            mNeedRefresh = bundle.getBoolean(Constant.WEB_VIEW_FRAGMENT_NEED_REFRESH)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireContext()).apply {
            setContent {
                setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
                InternalComposeWebView(url = mUrl)
            }
        }
    }
}